package com.example.mvvmdemo.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.example.mvvmdemo.R;
import com.example.common.base.BaseActivity;
import com.example.mvvmdemo.databinding.ActivityLanguageBinding;
import com.example.mvvmdemo.viewmodel.LanguageViewModel;
import com.example.common.utils.LocaleManager;
import com.example.common.utils.LogUtil;

public class LanguageActivity extends BaseActivity<ActivityLanguageBinding, LanguageViewModel> {
    
    @Override
    protected int getLayoutId() {
        return R.layout.activity_language;
    }
    
    @Override
    protected Class<LanguageViewModel> getViewModelClass() {
        return LanguageViewModel.class;
    }
    
    @Override
    protected LanguageViewModel createViewModel() {
        LanguageViewModel viewModel = new ViewModelProvider(this).get(LanguageViewModel.class);
        viewModel.setContext(this);
        return viewModel;
    }
    
    @Override
    protected void initView() {
        binding.setViewModel(viewModel);
        
        // 返回按钮
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 中文选项
        binding.layoutChinese.setOnClickListener(v -> {
            LogUtil.d("LanguageActivity", "Chinese selected");
            viewModel.selectLanguage(LocaleManager.SUPPORTED_LANGUAGES[0]); // 中文
        });
        
        // 英文选项
        binding.layoutEnglish.setOnClickListener(v -> {
            LogUtil.d("LanguageActivity", "English selected");
            viewModel.selectLanguage(LocaleManager.SUPPORTED_LANGUAGES[1]); // 英文
        });
        
        // 更新RadioButton状态
        updateRadioButtons();
    }
    
    @Override
    protected void initData() {
        // ViewModel会自动初始化当前语言
    }
    
    @Override
    protected void initObserver() {
        // 观察语言选择变化
        viewModel.selectedLanguage.observe(this, new Observer<LocaleManager.LanguageOption>() {
            @Override
            public void onChanged(LocaleManager.LanguageOption languageOption) {
                updateRadioButtons();
                LogUtil.d("LanguageActivity", "Selected language updated: " + languageOption.displayName);
            }
        });
        
        // 观察语言切换事件
        viewModel.languageChanged.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean changed) {
                if (changed != null && changed) {
                    showRestartDialog();
                }
            }
        });
    }
    
    private void updateRadioButtons() {
        binding.rbChinese.setChecked(viewModel.isChineseSelected());
        binding.rbEnglish.setChecked(viewModel.isEnglishSelected());
    }
    
    private void showRestartDialog() {
        new AlertDialog.Builder(this)
                .setTitle(R.string.language_change_success)
                .setMessage(R.string.language_change_tip)
                .setPositiveButton(R.string.language_restart_now, (dialog, which) -> {
                    restartApplication();
                })
                .setNegativeButton(R.string.language_restart_later, (dialog, which) -> {
                    finish();
                })
                .setCancelable(false)
                .show();
    }
    
    private void restartApplication() {
        LogUtil.d("LanguageActivity", "Restarting application");
        
        // 重启应用
        Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finishAffinity();
        }
    }
}